package com.tsd.common.service.impl;

import com.tsd.common.dao.CommonParameterValueMapper;
import com.tsd.common.entity.CommonParameterValue;
import com.tsd.common.entity.CommonParameterValueExt;
import com.tsd.common.service.CommonParameterValueService;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 描述：CommonParameterValue 服务实现层
 *
 * @author zsy
 * @date 2019/12/06 15:25:25
 */
@Service
public class CommonParameterValueServiceImpl extends BaseServiceImpl implements CommonParameterValueService {

    @Resource
    private CommonParameterValueMapper commonParameterValueMapper;

    @Override
    public CommonParameterValue getCommonParameterValueById(Long id) throws Exception {
        return commonParameterValueMapper.selectByPrimaryKey(id);
    }

    @Override
    public CommonParameterValue queryBySid(String sid) throws Exception {
        return commonParameterValueMapper.selectBySid(sid);
    }

    @Override
    public void deleteCommonParameterValue(Long id, SysUser opUser) throws Exception {
        CommonParameterValue old = commonParameterValueMapper.selectByPrimaryKey(id);
        super.checkEmpty(old, "该记录不存在");
        commonParameterValueMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveCommonParameterValue(CommonParameterValueExt record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId()) && !HlpUtils.isEmpty(record.getSid())) {
            CommonParameterValueExt valueExt = commonParameterValueMapper.selectBySid(record.getSid());
            if (valueExt != null) {
                record.setId(valueExt.getId());
            }
        }
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            if (record.getSn() == null) {
                record.setSn(1);
            }
            record.setState(1);
            commonParameterValueMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            commonParameterValueMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public List<CommonParameterValue> findCommonParameterValues(Map<String, Object> params, PageBean page) throws Exception {
        return commonParameterValueMapper.selectByPage(params, page);
    }

    @Override
    public List<CommonParameterValue> findCommonParameterValues(Map<String, Object> params) throws Exception {
        return commonParameterValueMapper.selectByParams(params);
    }

    @Override
    public CommonParameterValue getCommonParameterValueBySid(String sid) throws Exception {
        return commonParameterValueMapper.selectBySid(sid);
    }

    @Override
    public void deleteCommonParameterValues(List<String> list, SysUser opUser) throws Exception {
        for (String sid : list) {
            CommonParameterValue old = commonParameterValueMapper.selectBySid(sid);
            super.checkEmpty(old, "该记录不存在");
            super.autoInjectBaseData(old, opUser, TYPE_DELETE);
            commonParameterValueMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public List<CommonParameterValue> queryByKey(Map<String, Object> params) throws Exception {
        return commonParameterValueMapper.selectByKey(params);
    }
}
